package top.shiyiri.shops.controller;

import com.alibaba.fastjson.JSONObject;
import top.shiyiri.shops.bean.ProHotCateTop3;
import top.shiyiri.shops.service.ProHotCateTop3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author Aunean
 * @date 2021/12/21
 */
@Controller
public class ProHotCateTop3Controller {

    @Autowired
    private ProHotCateTop3Service service;

    @RequestMapping("/findProHotCateTop3")
    @ResponseBody
    public String findProHotCateTop3(int page, HttpSession session) {

        List<ProHotCateTop3> list = service.findProHotCateTop3(page, 6);
        String jsonArr = JSONObject.toJSONString(list);

        //查询总行数
        int lines = service.totalLine();

        //计算总页数
        int pages = lines % 6 == 0? lines/6: lines/6+1;
//        System.out.println(pages);
        //当前页
        session.setAttribute("pages", pages);
        session.setAttribute("page", page);

        return jsonArr;
    }

    @RequestMapping("/getPages")
    @ResponseBody
    public Integer getPages(HttpSession session) {

        int pages = (Integer) session.getAttribute("pages");

        return pages;
    }

    @RequestMapping("/findAllProHotCate")
    @ResponseBody
    public String findAllProHotCate() {
        List<ProHotCateTop3> list = service.findAllProHotCate();
        String jsonArr = JSONObject.toJSONString(list);
        return jsonArr;
    }
}
